package com.swagger.demo.admin;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.lastb7.swagger.enumeration.ApiEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author: lbq
 * @email: 526509994@qq.com
 * @date: 2022/6/24
 */
@Path("/file")
@Api(tags = "附件上传")
public class FileController extends Controller {

    @ApiOperation(value = "新建个人签名", consumes = ApiEnum.CONSUMES_FORM_DATA, httpMethod = ApiEnum.METHOD_POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "签名图片", required = false),
    })
    public void test1() {

        UploadFile file = this.getFile();

        this.renderText(file.getFileName());
    }

    @ApiOperation(value = "PNG下载", produces = ApiEnum.PRODUCES_PNG)
    public void downloadPng() {
        File file = new File(PathKit.getWebRootPath() + "/temp/test.png");

        this.renderFile(file);
    }

    @ApiOperation(value = "Svg下载", produces = ApiEnum.PRODUCES_FILE)
    public void downloadSvg() {
        File file = new File(PathKit.getWebRootPath() + "/temp/test.svg");

        this.renderFile(file);
    }

    @ApiOperation(value = "Excel下载[renderFile]", produces = ApiEnum.PRODUCES_FILE)
    public void downloadExcel() throws IOException {
        HttpServletResponse response = this.getResponse();

        File file = new File(PathKit.getWebRootPath() + "/temp/test.xlsx");

        this.renderFile(file);
    }

    @ApiOperation(value = "Excel下载[流下载]", produces = ApiEnum.PRODUCES_FILE)
    public void downloadExcel2() throws IOException {
        HttpServletResponse response = this.getResponse();

        File file = new File(PathKit.getWebRootPath() + "/temp/test.xlsx");

        response.setContentType("application/octet-stream");
        // UI点下载文件 正常下载文件名
//        response.addHeader("Content-Disposition", "attachement;filename=test.xlsx");

        // UI点下载文件, 始终为Knife4j.txt
//        response.addHeader("Content-Disposition", "attachement; filename=test.xlsx");

        // UI点下载文件, 下载文件名为 _test.xlsx_
//        response.addHeader("Content-Disposition", "attachement;filename=\"test.xlsx\"");

        // UI点下载文件, 始终为Knife4j.txt (JFinal renderFile 生成的格式)
        response.addHeader("Content-Disposition", "attachement; filename=\"test.xlsx\"");

        OutputStream outputStream = response.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        int i = -1;
        byte[] b = new byte[1024 * 1024];
        while ((i = fileInputStream.read(b)) != -1) {
            outputStream.write(b, 0, i);
        }
        fileInputStream.close();
        outputStream.close();

        this.renderNull();
    }
}
